package cn.wolfcode.luowowo.member.service.impl;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.util.AssertUtil;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.mapper.UserInfoMapper;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;


@Service
public class UserInfoServiceImpl implements IUserInfoService{
    @Autowired  //组件与组件间调用
    private UserInfoMapper userInfoMapper;
    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @Override
    public UserInfo get(long id) {
        return userInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPhone(String phone) {
        int count = userInfoMapper.selectByphone(phone);
        return count>0;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //设置验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "").
                substring(0, 4);
        //设置key
        String key ="v_"+phone;
        //缓存到redis中 TODO
        userInfoRedisService.setVerfiyCode(key,code,Consts.VERIFY_CODE_VAI_TIME);
        //假装已经发送了
        System.out.println("验证码是"+code+"请在"+ Consts.VERIFY_CODE_VAI_TIME+"分钟内使用");
    }

    @Override
    public void userRegist(String phone, String nickname, String password, String rpassword, String verifyCode)throws LogicException{
        AssertUtil.hasLength(phone,"手机号不能为空!");
        AssertUtil.hasLength(nickname,"昵称不能为空!");
        AssertUtil.hasLength(password,"密码不能为空!");
        AssertUtil.hasLength(rpassword,"确认密码不能为空!");
        AssertUtil.hasLength(verifyCode,"验证码不能为空!");

        //判断密码是否相同
        AssertUtil.isEquels(password,rpassword,"两次密码不一致!");

        //手机格式判断

        //手机唯一
        if(this.checkPhone(phone)){
            throw new LogicException("手机号码已经被注册!");
        }
        //获取验证码比较
        String key ="v_"+phone;
        String code = userInfoRedisService.getVerfiyCode(key);
        //忽略大小写
        if(code==null || !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或错误!");
        }

        UserInfo user = new UserInfo();
        user.setPhone(phone);
        user.setNickname(nickname);
        user.setState(UserInfo.STATE_NORMAL);
        user.setLevel(1);
        user.setCity("广州");
        user.setPassword(password);
        user.setHeadImgUrl("/images/default.jpg");
        userInfoMapper.insert(user);
    }

    @Override  //返回token
    public String userLogin(String username, String password) throws LogicException {
       //查询用户
        UserInfo userInfo = userInfoMapper.selectByusernameAndpassword(username,password);
        //判断用户是否存在
        if(userInfo==null){
            throw new LogicException("账号或密码错误!");
        }
        //如果存在就创建key+uuid
        String token = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 4);
        String key = RedisKeys.USER_LOGIN.join(token);
        //将user对象存进redis
        userInfoRedisService.addtoken(key,userInfo);
        return key;
    }

    @Override
    public List<UserInfo> list() {
        List<UserInfo> list = userInfoMapper.selectAll();
        return list;
    }

}
